package leetcodecompetition.D356;

import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-07-30-10:52
 * @Description: 1.0
 */
public class T6918 {
    public String minimumString(String a, String b, String c) {
        return minimumString("", 0, 0, List.of(a, b, c));
    }

    private String minimumString(String curr, int index, int mask, List<String> list) {
        if (index == 3) {
            return curr;
        }
        String s = null;
        for (int i = 0; i < 3; i++) {
            for (int j = list.get(i).length(); (mask & 1 << i) == 0 && j >= 0; j--) {
                if (curr.endsWith(list.get(i).substring(0, j)) || curr.contains(list.get(i))) {
                    String n = minimumString(curr + list.get(i).substring(j), index + 1, mask | 1 << i, list);
                    s = s == null || s.length() > n.length() || s.length() == n.length() && s.compareTo(n) > 0 ? n : s;
                    break;
                }
            }
        }
        return s;
    }

    @Test
    public void test() {
        System.out.println(minimumString("abc","bca","aaa"));
    }
}
